package com.project.jdbc.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
/**
 * 读取Properties配置文件
 * */
public class ConfigPropUtil extends BaseLogger {
	protected static Properties config;
	
	public ConfigPropUtil(String path){
		ClassLoader classLoader = ConfigPropUtil.class.getClassLoader();
		InputStream in = classLoader.getResourceAsStream(path);
		config = new Properties();
		try {
			config.load(in);
			in.close();
		} catch (IOException e) {
			logger.error(e);
			e.printStackTrace();
		}
	}


	public static String getProperty(String key) {
		String value = config.getProperty(key);
		return value == null ? value : value.trim();
	}

	public static String getProperty(String key, String defaultValue) {
		String value = config.getProperty(key);
		if (value == null)
			return defaultValue;
		return value.trim();
	}

	public static boolean getBooleanProperty(String name) {
		return getBooleanProperty(name, false);
	}

	public static boolean getBooleanProperty(String name, boolean defaultValue) {
		String value = getProperty(name);
		if (StringUtils.isBlank(value)) {
			return defaultValue;
		}
		return Boolean.valueOf(value);
	}

	public static Integer getIntProperty(String name) {
		return getIntProperty(name, null);
	}

	public static Integer getIntProperty(String name, Integer defaultValue) {
		String value = getProperty(name);
		if (value == null)
			return defaultValue;
		return Integer.valueOf(value);
	}

	public static Enumeration<Object> keys() {
		return config.keys();
	}

	public static Properties getPropertiesStartingWith(String startingWith) {
		Properties props = new Properties();
		for (Enumeration<Object> it = config.keys(); it.hasMoreElements();) {
			String key = (String) it.nextElement();
			props.put(key, config.get(key));
		}
		return props;
	}
}
